package com.websocket2.config;

import com.websocket2.listener.StompDisconnectEventListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry){
        //配置消息代理
        //前缀为/topic的消息将转发给消息代理再广播给当前连接的客户端
        //前缀为/queue代表点对点消息
        registry.enableSimpleBroker("/topic","/queue");
        //凡是客户端发送消息的路径的前缀为/app的都交给控制器中@MessageMapping注解的方法进行处理
        registry.setApplicationDestinationPrefixes("/app");
    }

    @Override  //创建WebScocket端点，供客户端连接用，该端点的完整URL将是http://localhost:8080/webSocket
    public void registerStompEndpoints(StompEndpointRegistry registry){
        registry.addEndpoint("/webSocket").withSockJS();
    }



    @Bean   //断开连接监听器
    public StompDisconnectEventListener STOMPDisconnectEventListener(){
        return new StompDisconnectEventListener();
    }

}
